package com.oncliedisk.administrator.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.oncliedisk.administrator.adapter.FragPagerAdapter;
import com.oncliedisk.administrator.onlinedisk.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/12/3.
 */
public class FragMentTransmission extends Fragment implements View.OnClickListener, ViewPager.OnPageChangeListener {

    private TextView tran_down;
    private TextView tran_up;
    private ViewPager pager;
    private List<Fragment> data;
    private FragPagerAdapter adapter;
    private FragDown down;
    private FragUp up;
    private Activity activity;

    public FragMentTransmission(Activity activity) {
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_transmission, null);
        initWeight(view);
        initData();
        initListener();
        return view;
    }

    private void initListener() {
        tran_down.setOnClickListener(this);
        tran_up.setOnClickListener(this);
        pager.setOnPageChangeListener(this);
    }

    private void initData() {
        down = new FragDown(activity);
        up = new FragUp(activity);
        data.add(down);
        data.add(up);
        adapter = new FragPagerAdapter(getFragmentManager(), data);
        pager.setAdapter(adapter);
    }

    private void initWeight(View view) {
        tran_down = (TextView) view.findViewById(R.id.tran_down);
        tran_up = (TextView) view.findViewById(R.id.tran_up);
        data = new ArrayList<Fragment>();
        pager = (ViewPager) view.findViewById(R.id.transmission_pager);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tran_down:
                pager.setCurrentItem(0);
                tran_down.setTextColor(getResources().getColor(R.color.blue));
                tran_up.setTextColor(getResources().getColor(R.color.grey));
                break;
            case R.id.tran_up:
                pager.setCurrentItem(1);
                tran_up.setTextColor(getResources().getColor(R.color.blue));
                tran_down.setTextColor(getResources().getColor(R.color.grey));
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            tran_down.setTextColor(getResources().getColor(R.color.blue));
            tran_up.setTextColor(getResources().getColor(R.color.grey));
        } else {
            tran_up.setTextColor(getResources().getColor(R.color.blue));
            tran_down.setTextColor(getResources().getColor(R.color.grey));
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

}
